package com.bxd.baodan.adapter;

import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.j256.ormlite.dao.Dao;
import com.bxd.baodan.R;
import com.bxd.baodan.app.Constant;
import com.bxd.baodan.base.BaseBaseAdapter;
import com.bxd.baodan.db.DBHelper;
import com.bxd.baodan.item.MessageItem;
import com.bxd.baodan.util.DateUtil;
import com.bxd.baodan.util.GlideUtil;
import com.bxd.baodan.util.LogUtil;
import com.bxd.baodan.widget.ShapedImageView;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 消息适配器
 *
 * @author oldfeel
 *         <p/>
 *         Create on: 2014年7月16日
 */
public class MessageAdapter extends BaseBaseAdapter<MessageItem> {
    public int type;
    public String[] msgType;
    Dao<MessageItem, ?> dao;

    public MessageAdapter(Context context, String[] type) {
        super(context, R.drawable.empty_person);
        this.msgType = type;
    }

    class ViewHolder {
        // tvNoReadCount
        private TextView tvContent, time, tvTitle, tag_msg;
        private ShapedImageView ivMsg;
        private ImageView noReadIv;
        private LinearLayout main_msg;

    }

    @Override
    public View getView(int position, View view) {
        final MessageItem item = getItem(position);

        ViewHolder holder = null;
        // 如果是任务消息，使用另外一个布局
        if (holder == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.msg_sys_item, null);


            holder.noReadIv = getImageView(view, R.id.msg_isread_icon);
            holder.tvContent = getTextView(view, R.id.message_content);
            holder.main_msg = getLinearLayout(view, R.id.main_msg);
            holder.tag_msg = getTextView(view, R.id.tag_msg);
            holder.time = getTextView(view, R.id.message_time);
            holder.tvTitle = getTextView(view, R.id.text_title);
            holder.ivMsg = (ShapedImageView) view.findViewById(R.id.img_msg);
            view.setTag(holder);

        } else {
            holder = (ViewHolder) view.getTag();
        }


        type = item.getType();

            holder.ivMsg.setVisibility(View.VISIBLE);
            GlideUtil.loadImage(context, item.getImage(), holder.ivMsg);

        holder.tvTitle.setText(item.getTitle());


        holder.tvContent.setText(item.getContent());
        holder.time.setText(DateUtil.getTimeStamp(item.getTime()));
        if (item.isIsread()) {
            holder.tag_msg.setBackgroundColor(context.getResources().getColor(R.color.text_gray));
            holder.tvTitle.setTextColor(context.getResources().getColor(R.color.text_gray));

        } else {
            holder.tag_msg.setBackgroundColor(context.getResources().getColor(R.color.green_lab));
            holder.tvTitle.setTextColor(context.getResources().getColor(R.color.black));
        }

        return view;
    }

    List<MessageItem> daoList = new ArrayList<>();

    @Override
    public void getData(int page) {
// TODO: 17/7/29   1 读取消息数
        try {
            if (page == 0) {
                list.clear();
            }
            Dao<MessageItem, ?> dao = DBHelper.getInstance(context).getDao(MessageItem.class);
            daoList = dao.queryBuilder().offset(page * Constant.PAGE_SIZE)
                    .limit(Constant.PAGE_SIZE).orderBy("time", false).where().
                            in("type", msgType).query();


//// TODO: 17/7/29  把原来已读的消息设置为已读
            for (MessageItem messageItem : list) {
                messageItem.setNoReadCount(DBHelper.getInstance(context).getNoReadCount(messageItem));
            }
            addAll(daoList);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除本条消息
     */
    private void SetCancel(final MessageItem item) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage("您确定要删除本条消息吗?")
                .setTitle("提示")
                .setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                delete(item);
                                dialog.dismiss();
                            }
                        }).setNegativeButton("取消", null).show();
    }

    public void delete(MessageItem item) {

        try {
            dao = DBHelper.getInstance(context).getDao(MessageItem.class);
            int clums = dao.delete(item);
            LogUtil.e("删除消息行数等于" + clums);
            cleanNotificationByMessageItemId(item);
            remove(item);
            setNotift();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void cleanNotificationByMessageItemId(MessageItem item) {
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancel(item.getId());
    }

    public void setNotift() {
        notifyDataSetChanged();
    }
}